package ru.niir.goodfil.ui.utils.excel;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import ru.niir.goodfil.db.Good;
import ru.niir.goodfil.ui.models.TopProductModel;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Волков Олег on 23.07.15.
 */
public class TopProductsUnload {
    public static void topProductsListToExcel(List<TopProductModel> topProductModels) throws IOException {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet sheet = hssfWorkbook.createSheet("0");
        int j = 0;
        int i = 0;
        HSSFRow header = sheet.createRow(i);
        Cell code = header.createCell(j);
        code.setCellValue("Артикул");
        sheet.autoSizeColumn(j);
        j++;

        Cell c1count = header.createCell(j);
        c1count.setCellValue("На складе");
        sheet.autoSizeColumn(j);
        j++;

        Cell resCount = header.createCell(j);
        resCount.setCellValue("В резерве");
        sheet.autoSizeColumn(j);
        j++;

        Cell sales = header.createCell(j);
        sales.setCellValue("Продажи за 12 мес.");
        sheet.autoSizeColumn(j);
        j++;

        Cell salesPerMonth = header.createCell(j);
        salesPerMonth.setCellValue("Продажи в мес. средние");
        sheet.autoSizeColumn(j);
        j++;

        Cell stock = header.createCell(j);
        stock.setCellValue("Запас");
        sheet.autoSizeColumn(j);
        j++;

        for(TopProductModel productModel : topProductModels){
            j = 0;
            i++;
            HSSFRow row = sheet.createRow(i);

            Cell _code = row.createCell(j);
            _code.setCellValue(productModel.getGoodCode());
            j++;

            Cell _c1count = row.createCell(j);
            _c1count.setCellValue(productModel.getGoodCount());
            j++;

            Cell _resCount = row.createCell(j);
            _resCount.setCellValue(productModel.getGoodReservedCount());
            j++;

            Cell _sales = row.createCell(j);
            _sales.setCellValue(productModel.getSales());
            j++;

            Cell _salesPerMonth = row.createCell(j);
            _salesPerMonth.setCellValue(productModel.getAverageSalesPerMonth());
            j++;

            Cell _stock = row.createCell(j);
            _stock.setCellValue(productModel.getStock());
        }

        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=\"top_50.xls\"");
        // Запись
        hssfWorkbook.write(response.getOutputStream());
        // Закрытие потока
        facesContext.responseComplete();
    }
}
